<?php

class http{
    
    public function __construct() {
        
    }
    
    public function http_request($url,$postData=[],$file='') {
        if(!empty($file)){
            $postData['media'] = new CURLFile($file);
        }
        //初始化
        $ch = curl_init();//必须
        //设置请求url
        curl_setopt($ch, CURLOPT_URL, $url);//必须
        //请求头关闭
        curl_setopt($ch, CURLOPT_HEADER, 0);
        //请求得到的结果不是直接输出，而是以字符串的形式
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//必须
        //请求超时时间
        curl_setopt($ch, CURLOPT_TIMEOUT, 30);
        //设置浏览器型号
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 SE 2.X MetaSr 1.0');
        //证书不检查
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        //设置post请求
        //开启post
        if($postData){
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
        }
        
        //发起请求
        $data = curl_exec($ch);//必须
        //错误报告
        if(curl_errno($ch)){
            echo curl_error($ch);
        }
        //关闭请求
        curl_close($ch);//必须
        return $data;
    }
}
